package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.*;

import cn.gson.school.dao.jiaoxue.TeamClassDao;
import cn.gson.school.dao.jiaoxue.ZhangJieDao;
import cn.gson.school.dao.qiantai.TeamStudentDao;
import cn.gson.school.dao.qiantai.TeamsDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.jiaoxue.*;
import cn.gson.school.mappers.jiaoxue.TeamsMappers;
import cn.gson.school.mappers.jiaoxue.ZhangJieMappers;

@Controller
@RequestMapping("/team")
public class TeamController {

	@Autowired
	private TeamsMappers teamsMappers;

	@Autowired
	private TeamsDao teamsDao;
	
	@Autowired
	private TeamStudentDao teamStudentDao;

	@Autowired
	private TeamClassDao teamClassDao;
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> teamList(TeamEntity teamEntity,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rwos", defaultValue = "10") int pageSize) {
		System.out.println("模糊查询："+teamEntity);
		PageHelper.startPage(pageNum, pageSize);
		List<TeamEntity> list = teamsMappers.teamsList(teamEntity);
		PageInfo<TeamEntity> info = new PageInfo<>(list);
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	

	// 添加团队
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> saveTeam(@Valid TeamEntity teamEntity, 
			//@RequestParam(name = "right_select",required=false) String right_select,
			HttpServletRequest request, Model model,
			BindingResult br) {
	System.out.println("进入了TeamController=====================");
	System.out.println("团队编号是："+teamEntity.getTeamId());
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		} else {
			String teamId = request.getParameter("teamId");//团队编号
			String userinfo = request.getParameter("userinfo");//操作人编号
			String teamName = request.getParameter("teamName");// 获取团队名称
			String createName = request.getParameter("createName");// 获取操作人姓名
			String classId = request.getParameter("classId");// 获取班级编号
			String zuZhangId = request.getParameter("zuZhangId");// 获取组长
			String selectedClass = request.getParameter("selectedClass");// 获取组员
			System.out.println("zuyuan1"+selectedClass);
			TeamEntity team=null;
			if (teamId==null||"".equals(teamId)) {
				System.out.println("添加Team");
				//添加
				team=new TeamEntity();
				team.setCreaTime(new Date());
				team.setCreateId(Long.valueOf(userinfo));
				team.setDelete(1);
				team.setTeamName(teamName);
				teamsDao.save(team);
				//创建团队学生对象
				TeamStudentEntity teamStudentEntity=new TeamStudentEntity();
				teamStudentEntity.setTeamId(team.getTeamId());
				if (classId==null||classId=="") {
					obj.put("message", "必须要选择一个班级");
					return obj;
				}
				if (zuZhangId !=null&&!zuZhangId.equals("")&&!zuZhangId.equals("请选择")) {
					//添加组长
					TeamStudentEntity teamStudentEntity1=new TeamStudentEntity();
					teamStudentEntity1.setStudentId(Long.valueOf(zuZhangId));
					teamStudentEntity1.setStuidentity("组长");
					teamStudentEntity1.setTeamId(team.getTeamId());
					teamStudentDao.save(teamStudentEntity1);
				}
				if (selectedClass!=null&&!selectedClass.equals("")) {
					//添加组员
					 String[] x=selectedClass.split(",");
					 for (int i = 0; i < x.length; i++) {
				        	Long y=Long.parseLong(x[i].trim());
				        	TeamStudentEntity TeamStudentEntity=new TeamStudentEntity();
				        	TeamStudentEntity.setStudentId(y);
				        	TeamStudentEntity.setStuidentity("组员");
				        	TeamStudentEntity.setTeamId(team.getTeamId());
				        	teamStudentDao.save(TeamStudentEntity);
						}
				}
				//创建团队班级对象
				TeamClassEntity teamClassEntity=new TeamClassEntity();
				teamClassEntity.setTeamEntity(team.getTeamId());
				if (classId.equals("请选择")) {
					obj.put("message", "班级不能为空~");
					return obj;
				}
				teamClassEntity.setClassEntity(Long.valueOf(classId));
				teamClassDao.save(teamClassEntity);
			}else {
				System.out.println("编辑Team");
				//编辑
				team=teamsDao.findOne(Long.valueOf(teamId));
				/*team.setTeamId(Long.valueOf(teamId));
				team.setCreateId(Long.valueOf(userinfo));*/
				team.setDelete(1);
				team.setTeamName(teamName);
				teamsDao.save(team);
				System.out.println("编辑team成功1！");
				//创建团队学生对象
				List<TeamStudentEntity> teamStudentEntity1=teamStudentDao.findByTeamId(Long.valueOf(teamId));
				for (TeamStudentEntity teamStudentEntity : teamStudentEntity1) {
					System.out.println("teamStudentEntityEEE"+teamStudentEntity.getStudentId()+teamStudentEntity.getStuidentity());
					if (teamStudentEntity.getStuidentity().equals("组长")) {
						System.out.println("编辑team成功2！");
						teamStudentEntity.setStudentId(Long.valueOf(zuZhangId));
					}else{
						System.out.println("编辑team成功2WWW！");
						teamStudentDao.delete(teamStudentEntity);
						System.out.println("编辑team成功3！");
						System.out.println("zuyuan"+selectedClass);
						System.out.println("selectedClass"+selectedClass);
						//添加组员
						if (selectedClass==null||selectedClass=="") {
							System.out.println("组员是空的");
						}else {
							System.out.println("编辑team成功4！");
							//添加组员
							 String[] x=selectedClass.split(",");
							 for (int i = 0; i < x.length; i++) {
								 System.out.println("组员是："+x[i]);
						        	Long y=Long.parseLong(x[i].trim());
						        	TeamStudentEntity TeamStudentEntity=new TeamStudentEntity();
						        	TeamStudentEntity.setStudentId(y);
						        	TeamStudentEntity.setStuidentity("组员");
						        	TeamStudentEntity.setTeamId(team.getTeamId());
						        	teamStudentDao.save(TeamStudentEntity);
								}
						}
					}
				}
				//设置团队班级
				List<TeamClassEntity> teamClassEntities=teamClassDao.findByTeamEntity(Long.valueOf(teamId));
				for (TeamClassEntity teamClassEntity : teamClassEntities) {
					teamClassEntity.setClassEntity(Long.valueOf(classId));
					teamClassDao.save(teamClassEntity);
				}
			}
		}
		obj.put("success", true);
		return obj;
	}
	@GetMapping("delete/{id}")
	@ResponseBody
	// 指定使用jpa的事务管理器
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		TeamEntity teamEntity = teamsDao.findOne(id);
		teamEntity.setDelete(0);
		teamsDao.save(teamEntity);
		return obj;
	}
}
